﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Com;
using Core;
using Models;

namespace Controllers
{
    public class AdclassController : AdminBaseController
    {
        adclassCore core = new adclassCore();
        public ActionResult GetPageList(pageinfo pageinfo)
        {
            if (!HasQx("ad"))
            {
                return Error("没有权限！");
            }

            List<object> argsList = new List<object>();
            string where = string.Empty;
            //where += " and endtime>=now()";
            if (!string.IsNullOrEmpty(pageinfo.key))
            {
                where += " and title like @" + argsList.Count + "";
                argsList.Add($"%{pageinfo.key}%");
            }
            var list = core.GetPageList(pageinfo.page,pageinfo.pagesize, where, pageinfo.order+" "+pageinfo.esc, argsList.ToArray());
            return Success(list);
        }

        public ActionResult GetList()
        {
            if (!HasQx("ad"))
            {
                return Error("没有权限！");
            }
            return Success(core.GetList(0, "", "endtime desc", null));
        }
        [HttpPost]
        public ActionResult Del(int id)
        {
            if (!HasQx("ad"))
            {
                return Error("没有权限！");
            }
            if (id > 0)
            {
                if (core.Del(id))
                {
                    Log(curmanage.username+"删除广告分类ID="+id);
                    return Success("删除成功！");
                }

            }
            return Error("删除失败！");
        }
        [HttpPost]
        public ActionResult DelAll(List<adclassEntity> modelList)
        {
            if (!HasQx("ad"))
            {
                return Error("没有权限！");
            }
            string ids = string.Empty;
            foreach (var adclassEntity in modelList)
            {
                core.Del(adclassEntity);
                ids += adclassEntity.ID + ",";
            }
            ids = ids.TrimEnd(',');
            Log(curmanage.username + "删除广告分类ID in (" + ids+")");
            return Success("删除成功！");
        }
        [HttpPost]
        public ActionResult Save(adclassEntity adclass)
        {
            if (!HasQx("ad"))
            {
                return Error("没有权限！");
            }
            if (adclass.title == null)
            {
                return Error();
            }
            var model = core.Save(adclass);
            Log(curmanage.username + "保存广告分类"+model.ToJson());
            return Success(model);
        }
    }
}
